﻿using System;
using System.Web.UI.WebControls;
using MS.InformationManagement.Presenter.Pager;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class Pager : BaseUserControl, IPageView
    {
        private PagerPresenter _Presenter;
        private int _PageCount;
        private int _PageIndex;

        #region [IPageView Properties]
        public int TotalRecords { get; set; }

        public int PageIndex
        {
            get { return _PageIndex; }
            set
            {
                _PageIndex = value;
                ltrPagerInfo.Text = string.Format("Viendo la página {0} de {1}.", value + 1, PageCount);
            }
        }

        public int PageSize { get; set; }

        public int NumberOfPagesToDisplay { get; set; }

        public string PreviousLinkText
        {
            get { return lnbPrevious.Text; }
            set { lnbPrevious.Text = value; }
        }

        public string NextLinkText
        {
            get { return lnbNext.Text; }
            set { lnbNext.Text = value; }
        }

        public int PreviousPageIndex
        {
            get { return Convert.ToInt32(lnbPrevious.CommandArgument); }
            set { lnbPrevious.CommandArgument = value.ToString(); }
        }

        public int NextPageIndex
        {
            get { return Convert.ToInt32(lnbNext.CommandArgument); }
            set { lnbNext.CommandArgument = value.ToString(); }
        }

        public int PageCount
        {
            get { return _PageCount; }
            set
            {
                _PageCount = value;
                ltrPagerInfo.Text = string.Format("Viendo la página {0} de {1}.", PageIndex + 1, value);
            }
        }

        public bool DisplayPreviousLink
        {
            get
            {
                return lnbPrevious.Visible;
            }
            set
            {
                lnbPrevious.Visible = value;
            }
        }

        public bool DisplayNextLink
        {
            get
            {
                return lnbNext.Visible;
            }
            set
            {
                lnbNext.Visible = value;
            }
        }

        public bool DisplayPager
        {
            get
            {
                return rptPagerElements.Visible;
            }
            set
            {
                rptPagerElements.Visible = value;
                ltrPagerInfo.Visible = value;
            }
        }

        public System.Collections.Generic.IEnumerable<int> DisplayablePages
        {
            get
            {
                return ViewState["DisplayablePages"] as System.Collections.Generic.List<int>;
            }
            set
            {
                ViewState["DisplayablePages"] = value;
                rptPagerElements.DataSource = value;
                rptPagerElements.DataBind();
            }
        }

        #endregion

        #region [Event Handlers]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    _Presenter.RebuildPager();
                }
                catch (Exception ex)
                {
                    OnError(this, ex);
                }
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _Presenter = new PagerPresenter(this);
        }
        protected void lnk_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton pressedLink = (sender as LinkButton);
                int oldPageIndex = PageIndex;
                int newPageIndex = Convert.ToInt32(pressedLink.CommandArgument);
                PageIndex = newPageIndex;
                OnPageChanged(sender, new PageChangeEventArgs { NewPageIndex = newPageIndex, OldPageIndex = oldPageIndex });
            }
            catch (Exception ex)
            {
                OnError(this, ex);
            }
        }
        #endregion

        public delegate void PageChangeHandler(object sender, PageChangeEventArgs e);
        public event PageChangeHandler OnPageChanged;
        public override event ErrorHandler OnError;

        public void BuildPager()
        {
            _Presenter.RebuildPager();
        }







    }
}
